package FlyGame;

import javax.swing.*;
import java.awt.*;

public class MyPipe {
    int upPipex;
    int upPipey;

    int downPipex;
    int downPipey;
    int spacerom;

    int upPipeh;
    int Pipeh;
    int downPipeh;

    int upPipew;
    int Pipew;
    int downPipew;

    int upcount;
    int downcount;

    public Rectangle uprect;
    public Rectangle downrect;
    public int speed;

    public MyPipe() {
        speed = 3;
        upPipex = 250;
        downPipex = 250;
        upPipey = (int) (Math.random() * 100) + 100;
        spacerom = (int) (Math.random() * 50) + 80;
        downPipey = 500 - upPipey - spacerom;

        upPipeh = FlyPicture.UpPipe.getIconHeight();
        Pipeh = FlyPicture.Pipe.getIconHeight();
        downPipeh = FlyPicture.DownPipe.getIconHeight();

        upPipew = FlyPicture.UpPipe.getIconWidth();
        Pipew = FlyPicture.Pipe.getIconWidth();
        downPipew = FlyPicture.DownPipe.getIconWidth();

        upcount = (upPipey - upPipeh) / Pipeh + 1;
        downcount = (downPipey - downPipeh) / Pipeh + 1;

        uprect = new Rectangle();
        downrect = new Rectangle();
    }

    public Rectangle getUpRect() {
        return uprect;
    }

    public Rectangle getDownRect() {
        return downrect;
    }

    public void paintPipe(Graphics g, JPanel panel) {

        //绘制上柱子
        for (int j = 0; j < this.upcount; j++) {
            FlyPicture.Pipe.paintIcon(panel, g, this.upPipex, j * this.Pipeh);
        }
        FlyPicture.UpPipe.paintIcon(panel, g, this.upPipex, this.upPipey - this.upPipeh);

        //绘制上柱子的范围矩形
        this.uprect.x = this.upPipex;
        this.uprect.y = 0;
        this.uprect.width = this.upPipew;
        this.uprect.height = this.upPipey;

        //绘制下柱子
        for (int k = 0; k < this.downcount; k++) {
            FlyPicture.Pipe.paintIcon(panel, g, this.downPipex, 500 - (k + 1) * this.Pipeh);
        }
        FlyPicture.DownPipe.paintIcon(panel, g, this.downPipex, 500 - this.downPipey);

        //绘制下柱子的范围矩形
        this.downrect.x = this.downPipex;
        this.downrect.y = 500 - this.downPipey;
        this.downrect.width = this.downPipew;
        this.downrect.height = this.downPipey;
    }

}
